package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;

import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwVillageHouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.VillageHouseFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/fw/task/villageHouse")
@Slf4j
public class VillageHouseFormController {
    @Autowired
    VillageHouseFormService villageHouseFormService;

    /**
     * 农村建筑
     * @param fwVillageHouseEntity
     * @return
     */
    @PostMapping("villageHouseFrom")
    public RestResponse save(FwVillageHouseEntity fwVillageHouseEntity,String type){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            villageHouseFormService.saveForm(fwVillageHouseEntity,type);
            jsonObject.put("message","保存成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看农村住房宅详情
     * @param formId
     * @return
     */
    @GetMapping("findVillageHouseById")
    public RestResponse findCityById(String formId){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            FwVillageHouseEntity cityById = villageHouseFormService.findVillageHouseById(formId);
            jsonObject.put("message","查询成功");
            restResponse=RestResponse.succeed(cityById);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改表单
     * @param type
     * @param fwVillageHouseEntity
     * @return
     */
    @PutMapping("{type}/updCity")
    public RestResponse updCity(@PathVariable String type,@RequestBody FwVillageHouseEntity fwVillageHouseEntity){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            villageHouseFormService.updVillCity(type,fwVillageHouseEntity);
            jsonObject.put("message","修改成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="修改成功";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村住宅结构类型饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villageStructure")
    public RestResponse cityStructure(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"砖（砌块）混", "低层框架", "轻钢（钢）装配式", "混凝土装配式", "砖木","土木", "石木","木结构","窑洞","混杂结构(砖/砌块/土/石)","其它"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 结构类型查询参数变化
                analysisQueryVo.setStructureType(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 结构类型查询参数变化
                analysisQueryVo.setStructureType(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村住宅年代饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("cityYear")
    public RestResponse cityYear(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"70s以前", "70s", "80s", "90s", "00s", "10s"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 年代查询参数变化
                analysisQueryVo.setBuildingTime(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 结构类型查询参数变化
                analysisQueryVo.setBuildingTime(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村住宅抗震设防烈度饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villageFortificationIntensity")
    public RestResponse villageFortificationIntensity(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"未设防", "6度", "7度", "8度", "9度", "10度"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 震设防烈度查询参数变化
                analysisQueryVo.setNowFortificationIntensity(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 震设防烈度查询参数变化
                analysisQueryVo.setNowFortificationIntensity(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村住宅抗震构造措施饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villageHouseMeasures")
    public RestResponse villageHouseMeasures(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"基本完备", "部分具备", "完全没有"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 抗震设防烈度查询参数变化
                analysisQueryVo.setHouseMeasures(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 抗震设防烈度查询参数变化
                analysisQueryVo.setNowFortificationIntensity(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村住宅曾受灾情况饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villageHouseDisasterType")
    public RestResponse villageHouseDisasterType(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"地震", "洪水", "台风","雪灾","火灾","其他"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 曾受灾情况查询参数变化
                analysisQueryVo.setOnceAffected(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 曾受灾情况查询参数变化
                analysisQueryVo.setOnceAffected(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 农村住宅抗震设防情况饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villageFortificationResult")
    public RestResponse villageFortificationResult(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"满足抗震设防要求且质量较好 ", "不满足抗震设防要求且质量很差", "需要进一步鉴定"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 抗震设防情况查询参数变化
                analysisQueryVo.setResult(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 抗震设防情况查询参数变化
                analysisQueryVo.setResult(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("noCityNum", resultCount);
            jsonObject.put("noCityArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村公共建筑安全隐患饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villageSafeHidden")
    public RestResponse villageSafeHidden(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"有", "无"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 安全隐患查询参数变化
                analysisQueryVo.setIsDamage(structureType);
                long count = villageHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 安全隐患查询参数变化
                analysisQueryVo.setIsDamage(structureType);
                long count = villageHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     * @param fwVillageHouseEntity
     * @return
     */
    @PostMapping("update")
    public RestResponse update(@RequestBody FwVillageHouseEntity fwVillageHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villageHouseFormService.update(fwVillageHouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
